package com.cqut.forum.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.cqut.forum.dao.ArticleDao;
import com.cqut.forum.dao.ArticleReplyDao;
import com.cqut.forum.service.GardenService;
import com.cqut.forum.service.ModuleService;
import com.cqut.forum.util.MapKey;

/**
 * gardenService 实现类
 * @author yangxun
 */
@Component
public class GardenServiceImp implements GardenService {
	@Resource
	private ArticleDao articleDao;
	@Resource
	private ArticleReplyDao articleReplyDao;
	@Resource
	private ModuleService moduleService;
	@SuppressWarnings("deprecation")
	@Override
	public Map<String, Object> getBallInfo(int articleId) {
		
		Map<String, Object> article = articleDao.getById(articleId);
		if(article == null){
			return null;
		}
		Map<String, Object> articleInfo = pack(article);

		List<Integer> articleIds = new ArrayList<Integer>();
		articleIds.add(articleId);
		List<Map<String, Object>> reply = articleReplyDao.getArticleReplys(articleIds);
		
		List<Map<String, Object>> replyInfo = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> map:reply){
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("image", map.get("user_photo"));
			m.put("name", map.get("user_name"));
			m.put("content", map.get("article_reply_contents"));
			m.put("time", ((Date)map.get("article_reply_time")).toLocaleString());
			replyInfo.add(m);
		}
		articleInfo.put("reply", replyInfo);
		
		return articleInfo;
	}

	@Override
	public List<Map<String, Object>> getLastBalls(int index,int size) {
		int moduleId = moduleService.getModuleIndex("花园");
		//List<Map<String, Object>> list = articleDao.get(0, size, moduleId);
		List<Map<String, Object>> list = articleDao.loadGardens(1,"","","",moduleId,index,size);
		
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> m:list){
			/*Map<String, Object> map = new HashMap<String, Object>();
			map.put("articleId", m.get("article_id"));
			map.put("userPhoto", m.get("user_photo"));
			map.put("articleGroupName", m.get("article_group_name"));
			map.put("visitTimes", m.get("visit_times"));
			map.put("replyTimes", m.get("review_times"));
			map.put("createTime", ((Date)m.get("publish_time")).toLocaleString());
			map.put("userName", m.get("user_name"));*/
			result.add(pack2(m));
		}
		
		return result;
	}
	@SuppressWarnings("deprecation")
	private Map<String, Object> pack(Map<String,Object> src){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("articleId", src.get("article_id"));
		map.put("image", src.get("user_photo"));
		map.put("name", src.get("user_name"));
		map.put("content", src.get("article_contents"));
		map.put("time", ((Date)src.get("publish_time")).toLocaleString());
		return map;
	}

	@Override
	public List<Map<String, Object>> loadGarden(String userName,
			String gardenType, String publishTime, int index, int size) {
		int moduleId = moduleService.getModuleIndex("花园");
		List<Map<String, Object>> gardens = articleDao.getArticles(userName,gardenType,publishTime,moduleId,index,size);
		return MapKey.changeMapKey(gardens);
	}

	@Override
	public List<Map<String, Object>> loadByCase(int userId,String publishTime,
			String ballKind, String userRelation, int index, int size) {
		int moduleId = moduleService.getModuleIndex("花园");
		List<Map<String, Object>> balls = articleDao.loadGardens(userId,publishTime,ballKind,userRelation,moduleId,index,size);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> m:balls){
			result.add(pack2(m));
		}
		return result;
	}
	
	@SuppressWarnings("deprecation")
	private Map<String, Object> pack2(Map<String,Object> m){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("articleId", m.get("article_id"));
		map.put("userPhoto", m.get("user_photo"));
		map.put("articleGroupName", m.get("article_group_name"));
		map.put("visitTimes", m.get("visit_times"));
		map.put("replyTimes", m.get("review_times"));
		map.put("createTime", ((Date)m.get("publish_time")).toLocaleString());
		map.put("userName", m.get("user_name"));
		map.put("kindSrc", m.get("article_password"));
		return map;
	}
}
